package day8;

import java.io.*;

public class FileDemo5 {

    public static void main(String[] args) throws IOException {
        String file = "C:\\d1\\";
        String file2 = "C:\\d2\\";

        int n = 100;

        for (int i = 0; i < n; i++) {

            File file3 = new File(file + i + ".txt");

            file3.createNewFile();

            Writer w = new FileWriter(file3);
            w.write("这是第" + i + "个txt");
            w.flush();
            w.close();

            File file4 = new File(file2 + i + ".txt");
            file4.createNewFile();

            copy(file3, file4);

        }

    }

    public static void copy(File file, File file2) throws IOException {
        FileInputStream fis = new FileInputStream(file);
        FileOutputStream fos = new FileOutputStream(file2);

        try {
            byte[] bbuf = new byte[102400];
            int hasRead = 0;
            while ((hasRead = fis.read(bbuf)) > 0) {
                fos.write(bbuf, 0, hasRead);
            }
        } catch (IOException E){
            E.printStackTrace();
        } finally {
            fos.flush();
            fis.close();
            fos.close();
        }
    }
}
